input = $ 'div.input'

acFlag = 0

loading = $ 'div.loading'


`var timer_hideMask`

$ document
	.ready ()->
		getCourse()
		$ 'li.add-course'
			.bind 'click',displayMask
		$ 'button#sure'
			.bind 'click',addCourse
		$ 'button.cancle'
			.bind 'click',()->
				if acFlag isnt 0
					manClose()
				else
					hideMask()
	

displayMask = ()->
	input.val("")
	$ 'div.log'
		.text '请输入课程名称'
	$ 'div.mask'
		.css 'display','block'

hideMask = ()->
	$ 'div.mask'
		.css 'display','none'

getCourse = ()->
	loading.css 'display','block'
	$ 'li'
		.remove '.course'
	$ 'li'
		.remove '.log'
	$.get '/teacher/getCourse',(data)->
		if data.head is 'ok'
			displayCourse course for course in data.info
		else
			log = $ '<li class="log"><li>'
				.text data.info
		loading.css 'display','none'

displayCourse = (course)->
	if course.del isnt true
		Course = $ '<li class="course" ></li>'
			.html '<a href=/teacher/course/' + course._id + '><span class="title">' + course.cname + '</span>'
		$('li.add-course').before(Course)
	false

manClose = ()->
	hideMask()
	acFlag = 0
	$ 'div.log'
		.after input
	clearTimeout timer_hideMask

addCourse = ()->
	if acFlag isnt 0
		manClose()
	else
		acFlag = 1
		cname = $ 'input[name=key]'
			.val()
		$.post '/teacher/addCourse','cname':cname,(data)->
			if data.head is 'ok'
				$ 'div.log'
					.html '成功添加课程<b>'+data.info.cname+'</b> 课程号为' + data.info.cid
				getCourse()
				input.remove()
				timer_hideMask = setTimeout(()->
					hideMask()
					$ 'div.log'
						.after input
					acFlag = 0
				,10000
				)
			else
				$ 'div.log'
					.text data.info
	

$ document
	.keydown (event)->
		if event.keyCode is 27
			hideMask()

